package bb.lanxing.adapter.sport;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import bb.lanxing.R;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import bb.lanxing.util.SpliceBitmapUtil;

public class SportRecordListAdapter extends RecyclerView.Adapter<SportRecordListAdapter.ViewHolder> {
    private List<Workout> workoutList;
    private OnItemClickListener listener;
    private Context mContext;

    public SportRecordListAdapter(List<Workout> workouts) {
        workoutList = workouts;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView typeView;
        TextView typeTextView;
        TextView distanceView;
        TextView hotView;
        ImageView cadenceTypeIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            typeView = itemView.findViewById(R.id.typeView);
            typeTextView = itemView.findViewById(R.id.typeTextView);
            distanceView = itemView.findViewById(R.id.distanceView);
            hotView = itemView.findViewById(R.id.hotView);
            cadenceTypeIcon = itemView.findViewById(R.id.cadenceTypeIcon);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sport_record, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (workoutList != null) {
            Workout workout = workoutList.get(position);
            if (workout != null) {
                String typeText = DateUtil.format(workout.getStartTime(), 1) + " ";
                switch (workout.getSport()) {
                    case Workout.TYPE_CYCLING:
                        holder.typeView.setImageResource(R.drawable.ic_data_active_ride);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.cycling));
                        break;
                    case Workout.TYPE_RUNNING:
                        holder.typeView.setImageResource(R.drawable.ic_data_active_run);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.running));
                        break;
                    case Workout.TYPE_WALKING:
                        holder.typeView.setImageResource(R.drawable.ic_data_active_walk_slowly);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.walking));
                        break;
                    case Workout.TYPE_SKIING:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_skiing);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.skiing));
                        break;
                    case Workout.TYPE_SWIMMING:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_swim_indoor);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.swimming));
                        break;
                    case Workout.TYPE_TRAINING:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_barbell_training);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.training));
                        break;
                    case Workout.TYPE_OTHER:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_car);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.other_sport));
                        break;
                    case Workout.TYPE_INDOOR_BIKE:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_indoor_bike);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.ftms_indoor_bike));
                        break;
                    case Workout.TYPE_BIKE_TRAINER:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_bike_trainer);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.device_biketrainer_title));
                        break;
                    case Workout.TYPE_ROWER:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_rower);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.device_rower_title));
                        break;
                    case Workout.TYPE_CROSS_TRAINER:
                        holder.typeView.setImageResource(R.drawable.ic_sport_data_crosstrainer);
                        holder.typeTextView.setText(typeText + mContext.getString(R.string.ftms_crosstrainer));
                        break;
                }
                holder.distanceView.setText(CommonUtil.getFormatDistance(workout.getDistance()) + mContext.getString(R.string.unit_ch_km));
                String string = mContext.getString(R.string.hot_not_upload);
                int color = mContext.getResources().getColor(R.color.grey_999999);
                if (workout.getServerId() > 0) {
                    string = Math.round(workout.getCredits()) + mContext.getString(R.string.hot_value);
                    color = mContext.getResources().getColor(R.color.global_orange_color);
                }
                holder.hotView.setText(string);
                holder.hotView.setTextColor(color);
                Drawable generateWorkoutFeatures = SpliceBitmapUtil.generateWorkoutFeatures(mContext,
                        workout.getCadenceSource(), workout.getHeartSource(), workout.getLocSource(),
                        workout.hasMatchedSegment(), workout.getSlopeGradeMax(), workout.isThreedWorkout());
                holder.cadenceTypeIcon.setVisibility(generateWorkoutFeatures != null ? View.VISIBLE : View.GONE);
                holder.cadenceTypeIcon.setImageDrawable(generateWorkoutFeatures);
                holder.itemView.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if (workoutList != null) {
            return workoutList.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void updateData(List<Workout> workouts) {
        workoutList = workouts;
        notifyDataSetChanged();
    }
}